package lesson2;

import lombok.SneakyThrows;

import java.util.concurrent.BlockingQueue;

public class Producer implements Runnable{

    public volatile Boolean flag = false;

    private BlockingQueue<Integer> blockingQueue;

    public Producer(BlockingQueue blockingQueue){
        this.blockingQueue = blockingQueue;
    }

    @SneakyThrows
    @Override
    public void run() {
        int num = 0;
        try {
            while (!flag){
                blockingQueue.put(num);
                System.out.println(num + "被放到队列。。");
                num ++;
            }

            /*while (!Thread.currentThread().isInterrupted()){
                blockingQueue.put(num);
                System.out.println(num + "被放到队列。。");
                num ++;
            }*/
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("生产者结束了运行");
        }
    }
}
